package main

import (
	"flag"
	"log"
	"time"
	"ups/dbs"
)

func main() {
	var isBuild bool
	var host string
	var port int64
	flag.BoolVar(&isBuild, "build", false, "依据当前目录中文件内容创建升级配置")
	flag.StringVar(&host, "host", "127.0.0.1", "升级服务IP地址")
	flag.Int64Var(&port, "port", 1978, "升级服务端口号")
	flag.Parse()
	// log.Println("isBuild=", isBuild)
	// log.Println("host=", host)
	// log.Println("port=", port)
	if isBuild {
		dbs.InitEnv()
		log.Println(">>>>>依据当前目录中文件内容创建升级配置")
		path := "." //tools.GetAppPath()
		files, err := dbs.BuildFiles(path)
		if err != nil {
			log.Println("\t组织文件列表发生错误：", err.Error())
			log.Printf("\t%s", time.Now().Format("2006-01-02 15:04:05"))
			return
		}
		if dbs.SaveFiles(files) {
			log.Println("\t升级配置创建完毕")
			log.Printf("\t%s", time.Now().Format("2006-01-02 15:04:05"))
			return
		}
	} else {
		log.Printf(">>>>>自动升级服务器[%s:%d]正在运行...", host, port)
		HandleRequests(host, port)
	}
}
